#include "qdlglogin.h"
#include "ui_qdlglogin.h"

//Add by dimon 2024/-0316 14:32, Copy the source code
#include "qdlggchat.h"
#include "common.h"
#include <QMessageBox> //弹出对话框
#include <QHostAddress>
#include "qdlgregister.h"

qdlglogin::qdlglogin(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::qdlglogin)
{
    ui->setupUi(this);

    //add by dimon, 2024-03-16 15:12
    serverOk = false;
    ui->pushbtnLogin->setEnabled(false);
    ui->cmdlinkRegister->setEnabled(false);

    //设置login对话框’用户名'输入文本对话框提示符
    ui->leditUsername->setPlaceholderText(QString::fromLocal8Bit("账号"));

    //设置login对话框’密码'输入文本对话框提示符,设置密码回显模式为'QLineEdit::Password'
    ui->lleditPasswd->setPlaceholderText(QString::fromLocal8Bit("密码"));
    ui->lleditPasswd->setEchoMode(QLineEdit::Password);

    //New申请一个 QTcpSocket对象
    tcpSocket = new QTcpSocket(this);

    //关联信号-槽
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));

}

qdlglogin::~qdlglogin()
{
    delete ui;
}

// 读取/处理 TCP 套接字数据
void qdlglogin::onReadyRead()
{
    QByteArray block = tcpSocket->readAll();
    QDataStream in(&block, QIODevice::ReadOnly);
    quint16 dataSize;
    quint8 msgType;

    //Message消息的定义为 {msgSize, msgType, MsgBody},
    //msgSize = msgType + msgBody
    in >> dataSize >> msgType;

    switch(msgType){
    case MSG_SERVER_LOGIN_SUCCESS:
    {
        QDlgGChat *dlg = new QDlgGChat(userid, ip, port);
        dlg->show();
        this->close();
        break;
    }
    case MSG_SERVER_LOGIN_ERR_PSW:
    {
        QMessageBox::information(NULL, tr("提示"), tr("密码错误。"));
        ui->lleditPasswd->clear();
        ui->lleditPasswd->setFocus();
        break;
    }
    case MSG_SERVER_LOGIN_ERR_ID:
    {
        QMessageBox::warning(NULL, tr("提示"), tr("该账号不存在，请先注册。"));
        ui->leditUsername->clear();
        ui->leditUsername->setFocus();
        ui->lleditPasswd->clear();
        break;
    }
    case MSG_SERVER_LOGIN_ERR_RELOGIN:
    {
        QMessageBox::warning(NULL, tr("提示"), tr("清不要重复登陆。"));
        ui->leditUsername->clear();
        ui->lleditPasswd->clear();
        ui->leditUsername->setFocus();
        break;
    }
    default:
        break;
    }

}

//处理"注册"的槽函数
void qdlglogin::on_cmdlinkRegister_clicked()
{
    qdlgregister dlgReg(ip, port, this);
    dlgReg.exec();

    //如果注册成功，则者皆登陆
    userid = dlgReg.userid;
    if(userid > 0){
        ui->leditUsername->setText(QString::number(userid));
        ui->lleditPasswd->setText(dlgReg.password);
        on_pushbtnLogin_clicked();
    }
}

//登陆
void qdlglogin::on_pushbtnLogin_clicked()
{
    if(ip.isEmpty() || port.isEmpty()){
        QMessageBox::warning(NULL, tr("提示"), tr("请先设置服务器端口和IP"));
    }
    else{
        userid = ui->leditUsername->text().trimmed().toUInt();
        password = ui->lleditPasswd->text().trimmed();

        //QTcpSocket连接到服务器Ip和port
        tcpSocket->abort();
        tcpSocket->connectToHost(QHostAddress(ip), (quint16)port.toUInt());
        quint8 msgType = MSG_CLIENT_LOGIN;
        //QByteArray 字节数组
        QByteArray block;
        //QDataStream 数据流和QByteArry字节数组关联起来
        QDataStream out(&block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_5_4);
        //'<<'插入运算符把 数据写入 数据流
        out<<(quint16)0 << (quint8)msgType << userid << password;
        //数据流 seek()回到数据流的起始位置（偏移量为0)
        out.device()->seek(0);
        out<<(quint16)(block.size() - sizeof(quint16));
        //把字符byteArray写到QTcpSocket
        tcpSocket->write(block);
    }
}

//确认服务器配置
void qdlglogin::on_btnServerOk_clicked()
{
    QString strIP = ui->lineIP->text().trimmed();
    QString strPort = ui->linePort->text().trimmed();

    //正则表达式
    QRegExp rxIp("\\d+\\.\\d+\\.\\d+\\.\\d+");
    QRegExp rxPort("[1-9]\\d{3,4}");
    rxIp.setPatternSyntax(QRegExp::RegExp);
    rxPort.setPatternSyntax(QRegExp::RegExp);

    if(!rxIp.exactMatch(strIP)){
        QMessageBox::critical(NULL, tr("错误"), tr("IP地址不合法"));
        ui->lineIP->setFocus();
        return;
    }

    if(!rxPort.exactMatch(strPort)){
        QMessageBox::critical(NULL, tr("错误"), tr("端口号不合法"));
        ui->linePort->setFocus();
        return;
    }

    ip = strIP;
    port = strPort;
    serverOk = true;
    //resize(350, 145);
    ui->pushbtnLogin->setEnabled(true);
    ui->cmdlinkRegister->setEnabled(true);

    //服务器配置成功，把服务器配置按钮disable
    ui->lineIP->setDisabled(true);
    ui->linePort->setDisabled(true);
    ui->btnServerOk->setDisabled(true);
    ui->btnServerCanel->setDisabled(true);
}

//关闭服务器配置
void qdlglogin::on_btnServerCanel_clicked()
{
    resize(350,145);
    ui->lineIP->clear();
    //ui->lineIP->setDisabled(true);
    ui->linePort->clear();
    //ui->linePort->setDisabled(true);
    //ui->btnServerOk->setDisabled(true);
    //ui->btnServerCanel->setDisabled(true);
}

void qdlglogin::on_btnServer_clicked()
{
    static bool serverSetup = true;

    if(serverSetup){
        resize(350, 145);
    }
    else{
        resize(350, 360);
        if(serverOk){
            ui->lineIP->setText(ip);
            ui->linePort->setText(port);
        }
        ui->lineIP->setDisabled(false);
        ui->linePort->setDisabled(false);
        ui->btnServerOk->setDisabled(false);
        ui->btnServerCanel->setDisabled(false);
    }

    serverSetup = !serverSetup;
}
